package com.gtlab1207.br_awareness.services;

import com.gtlab1207.br_awareness.common.AESUtil;
import com.gtlab1207.br_awareness.common.PageUtil;
import com.gtlab1207.br_awareness.models.dao.UserDao;
import com.gtlab1207.br_awareness.models.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息业务层
 */
@Service
public class UserService {
    @Autowired
    UserDao userDao;

    /**
     * 显示用户列表
     * @param params
     * @return
     */
    @Transactional
    public Map<String, Object> selectUser(Map params,Integer pageSize){
        Map<String, Object> map = new HashMap<>();
        List<User> result = userDao.selectUser(params);
        if(result!=null&&result.size()!=0){
            Integer count = userDao.getCount(params);
            Integer pageCount= PageUtil.setPageInfoPageSize(count,pageSize);
            map.put("data",result);
            map.put("pageCount",pageCount);
            map.put("all",count);
            map.put("total",result.size());
        }
        return map;
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @Transactional
    public Integer insertUser(User user){
        int result=0;
        if(user!=null){
            if(user.getPassword()!=null&&!user.getPassword().equals("")){
                String password = AESUtil.Encrypt(user.getPassword());
                user.setPassword(password);
            }
            result= userDao.insertUser(user);
        }
        return result;
    }


    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @Transactional
    public Integer updateUser(User user){
        int result=0;
        if(user.getId()!=null){
            if(user.getPassword()!=null&&!user.getPassword().equals("")){
                String password = AESUtil.Encrypt(user.getPassword());
                user.setPassword(password);
            }
            result=userDao.updateUser(user);
        }
        return result;
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @Transactional
    public Integer deleteUser(Integer id){
        int result=0;
        if (id!=null) {
            result=userDao.deleteUser(id);
        }
        return result;
    }

    /**
     * 通过手机号查找用户
     * @param phone
     * @return
     */
    public Map<String, Object> getUserByPhone(String phone){
        Map<String, Object> map = new HashMap<>();
        User user = userDao.getUserByPhone(phone);
        map.put("data", user);
        map.put("total", user==null?0:1);
        return map;
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Transactional
    public Map<String, Object> getUserById(Integer id){
        Map<String, Object> map = new HashMap<>();
        User user = null;
        if(id!=null)
            user = userDao.getUserById(id);
        map.put("data",user);
        map.put("total", user==null?0:1);
        return map;
    }

}
